package com.ynet.middleground.mobile.approve.mq.producer;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.dao.OperateMqLogMapper;
import com.ynet.middleground.mobile.approve.entity.OperateMqLog;
import com.ynet.middleground.mobile.approve.mq.SourceMobileApprove;

/**
 * @author liulx
 * @description 中台需要推送的审批操作前后数据
 * @date 2020-08-20 17:00
 */
@EnableBinding({SourceMobileApprove.class})
@Component
public class SourceMobileApproveProducer {

    @Autowired
    private SourceMobileApprove sourceMobileApprove;

    @Autowired
    private OperateMqLogMapper operateMqLogMapper;

    @Autowired
    private Mapper mapper;

    /**
     * 给核心系统 MQ 推送审批操作执行前数据
     *
     * @param data 审批操作执行前的数据
     */
    public void sendMessagesApproveBefore(Map<String, Object> data) {
        IfpLogger.info("mq", "MQ 给核心系统推送审批操作执行前数据，往 Binding-{} 推送信息-{}", SourceMobileApprove.JKZTYDSP_TO_BLXD_SPCZ,
            data);
        Map<String, Object> headers = new HashMap<>(2);
        headers.put("TAGS", "in");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<Map<String, Object>> message = MessageBuilder.createMessage(data, messageHeaders);
        log(data);
        this.sourceMobileApprove.noticeApprove().send(message);
    }

    /**
     * 给核心系统 MQ 推送审批操作执行后数据
     *
     * @param data 审批操作执行后的数据
     */
    public void sendMessagesApproveAfter(Map<String, Object> data) {
        IfpLogger.info("mq", "MQ 给核心系统推送审批操作执行后数据，往 Binding-{} 推送信息-{}", SourceMobileApprove.JKZTYDSP_TO_BLXD_SPCZ,
            data);
        Map<String, Object> headers = new HashMap<>(2);
        headers.put("TAGS", "out");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<Map<String, Object>> message = MessageBuilder.createMessage(data, messageHeaders);
        log(data);
        this.sourceMobileApprove.noticeApprove().send(message);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void log(Map<String, Object> msgMap) {
        OperateMqLog operateMqLog = mapper.map(msgMap, OperateMqLog.class);
        operateMqLog.setGmtCreate(LocalDateTime.now());
        operateMqLogMapper.insert(operateMqLog);
    }

    /**
     * 给核心系统 MQ 推送审批流程需要发送放款验证码<br />
     * 由渠道控制具体发送的短信内容和手机号
     *
     * @param data 当前审批流程相关数据
     */
    public void sendMessagesSendLendersCaptchaCode(Map<String, Object> data) {
        IfpLogger.info("mq", "MQ 给核心系统推送审批流程需要发送放款验证码，往 Binding-{} 推送信息-{}",
            SourceMobileApprove.JKZTYDSP_TO_BLXD_FSFKYZM, data);
        Map<String, Object> headers = new HashMap<>(2);
        headers.put("TAGS", "in");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<Map<String, Object>> message = MessageBuilder.createMessage(data, messageHeaders);
        log(data);
        this.sourceMobileApprove.noticeSendCaptcha().send(message);
    }

}
